﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KimPhongCRM.Models;
using DevExpress.Web.Mvc;

namespace KimPhongCRM.Controllers
{
    [PermissionFilter]
    public class TourKHController : BaseController
    {
        private Entities db = new Entities();
        List<KimPhongCRM.Models.sp_Select_Combobox_User_Result> listUser = new List<sp_Select_Combobox_User_Result>();
        List<KimPhongCRM.Models.sp_Select_Combobox_Tour_Result> listTour = new List<sp_Select_Combobox_Tour_Result>();
        List<KimPhongCRM.Models.sp_Select_Combobox_KhachHang_Result> listKhachHang = new List<sp_Select_Combobox_KhachHang_Result>();
        List<sp_Select_LuatTinhDiem_Result> listLuatTinhDiem = new List<sp_Select_LuatTinhDiem_Result>();

        public TourKHController()
        {
            listUser = db.sp_Select_Combobox_User().ToList();
            listTour = db.sp_Select_Combobox_Tour().ToList();
            listKhachHang = db.sp_Select_Combobox_KhachHang().ToList();
            listLuatTinhDiem = db.sp_Select_LuatTinhDiem(DateTime.Now).ToList();
        }
        //
        // GET: /TourKH/
        private void setViewData()
        {
            ViewData["listUser"] = listUser;
            ViewData["listTour"] = listTour;
            ViewData["listKhachHang"] = listKhachHang;
            ViewData["LuatTinhDiem"] = listLuatTinhDiem[0];
        }
        [MvcSiteMapProvider.MvcSiteMapNode(Title = "Chuyến đi", Key = "ChuyenDi", Clickable = true, ParentKey = "Home")]
        public ViewResult Index()
        {
            setViewData();
            return View(db.tblTourKHs.ToList());
        }
        public JsonResult SetTourInfo(long maTour)
        {
            Dictionary<string, long> info = new Dictionary<string, long>();
            tblTour tour = db.tblTours.Find(maTour);
            info.Add("diemSo", (long)(listLuatTinhDiem[0].tyLeTienDiem * tour.giaTien));
            info.Add("giaTour", (long)(tour.giaTien));
            return Json(info, JsonRequestBehavior.AllowGet);
        }
        //
        // GET: /TourKH/Details/5

        public ViewResult Details(long id)
        {
            tblTourKH tbltourkh = db.tblTourKHs.Find(id);
            return View(tbltourkh);
        }

        //
        // GET: /TourKH/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /TourKH/Create

        [HttpPost]
        public ActionResult Create(tblTourKH tbltourkh)
        {
            if (ModelState.IsValid)
            {
                db.tblTourKHs.Add(tbltourkh);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(tbltourkh);
        }

        //
        // GET: /TourKH/Edit/5

        public ActionResult Edit(long id)
        {
            tblTourKH tbltourkh = db.tblTourKHs.Find(id);
            return View(tbltourkh);
        }

        //
        // POST: /TourKH/Edit/5

        [HttpPost]
        public ActionResult Edit(tblTourKH tbltourkh)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tbltourkh).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(tbltourkh);
        }

        //
        // GET: /TourKH/Delete/5

        public ActionResult Delete(long id)
        {
            tblTourKH tbltourkh = db.tblTourKHs.Find(id);
            return View(tbltourkh);
        }

        //
        // POST: /TourKH/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(long id)
        {
            tblTourKH tbltourkh = db.tblTourKHs.Find(id);
            db.tblTourKHs.Remove(tbltourkh);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
        KimPhongCRM.Models.Entities db1 = new KimPhongCRM.Models.Entities();

        [ValidateInput(false)]
        public ActionResult ListTourKHPartial()
        {
            setViewData();
            var model = db1.tblTourKHs;
            return PartialView("_ListTourKHPartial", model.ToList());
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult ListTourKHPartialAddNew(KimPhongCRM.Models.tblTourKH item)
        {
            setViewData();
            var model = db1.tblTourKHs;
            if (ModelState.IsValid)
            {
                try
                {
                    model.Add(item);
                    int rs = db1.SaveChanges();
                    if (rs > 0)
                    {
                        db1.sp_Check_TheVip(item.maKH, item.nguoiSuaCuoi);
                    }
                }
                catch (Exception e)
                {
                    ViewData["EditError"] = e.Message;
                }
            }
            else
                ViewData["EditError"] = "Please, correct all errors.";
            return PartialView("_ListTourKHPartial", model.ToList());
        }
        [HttpPost, ValidateInput(false)]
        public ActionResult ListTourKHPartialUpdate(KimPhongCRM.Models.tblTourKH item)
        {
            setViewData();
            var model = db1.tblTourKHs;
            if (ModelState.IsValid)
            {
                try
                {
                    var modelItem = model.FirstOrDefault(it => it.maTourKH == item.maTourKH);
                    if (modelItem != null)
                    {
                        this.UpdateModel(modelItem);
                        int rs = db1.SaveChanges();
                        if (rs > 0)
                        {
                            db1.sp_Check_TheVip(item.maKH, item.nguoiSuaCuoi);
                        }
                    }
                }
                catch (Exception e)
                {
                    ViewData["EditError"] = e.Message;
                }
            }
            else
                ViewData["EditError"] = "Please, correct all errors.";
            return PartialView("_ListTourKHPartial", model.ToList());
        }
        [HttpPost, ValidateInput(false)]
        public ActionResult ListTourKHPartialDelete(System.Int64 maTourKH)
        {
            setViewData();
            var model = db1.tblTourKHs;
            if (maTourKH != null)
            {
                try
                {
                    var item = model.FirstOrDefault(it => it.maTourKH == maTourKH);
                    if (item != null)
                        model.Remove(item);
                    db1.SaveChanges();
                }
                catch (Exception e)
                {
                    ViewData["EditError"] = e.Message;
                }
            }
            return PartialView("_ListTourKHPartial", model.ToList());
        }
    }
}